/**
 * Created by daniel.arthur on 24/10/2014.
 */

import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonArray;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

public class JsonTest {
    public static JsonObject wifiJson = new JsonObject();

    public static void main(String[] args) {
        criaJson();
        mostraJson();
        post();
    }

    public static void criaJson() {
        JsonArray wJson = new JsonArray();

        JsonObject wifisJson = new JsonObject();
        wifisJson.add("ssid", "casa");
        wifisJson.add("bssid", "00:00:00:00:00");
        wifisJson.add("rssi", "-50");

        wJson.add(wifisJson);

        wifiJson.add("users", wJson);

    }

    public static void mostraJson() {
        System.out.println(wifiJson.toString());
    }

    public static void post() {
        URL url = null;
        try {
            url = new URL("http://localhost:4567/wifi");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Map<String,Object> params = new LinkedHashMap<>();
        params.put("name", "Freddie the Fish");
        params.put("email", "fishie@seamail.com");
        params.put("reply_to_thread", 10394);
        params.put("message", "Shark attacks in Botany Bay have gotten out of control. We need more defensive dolphins to protect the schools here, but Mayor Porpoise is too busy stuffing his snout with lobsters. He's so shellfish.");

        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            try {
                postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            postData.append('=');
            try {
                postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        byte[] postDataBytes = new byte[0];
        try {
            postDataBytes = postData.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(postDataBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*try {
            Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            for (int c; (c = in.read()) >= 0; System.out.print((char)c));
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}
